/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.momarketing.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.momarketing.entity.PageVisit;
import com.boxin.ims.modules.momarketing.service.PageVisitService;

/**
 * 页面访问Controller
 * @author Jakemanse
 * @version 2014-05-22
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/momarketing/pageVisit")
public class PageVisitController extends BaseController {

	@Autowired
	private PageVisitService pageVisitService;
	
	@ModelAttribute
	public PageVisit get(@RequestParam(required=false) Long id) {
		if (id != null){
			return pageVisitService.get(id);
		}else{
			return new PageVisit();
		}
	}
	
	@RequiresPermissions("momarketing:pageVisit:view")
	@RequestMapping(value = {"list", ""})
	public String list(PageVisit pageVisit, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			pageVisit.setUser(user);
		}
		
		if (pageVisit.getStartTime() != null) {
			pageVisit.setCreateTime(pageVisit.getStartTime());
		} else {
			pageVisit.setCreateTime(null);

		}
		Page<PageVisit> page = pageVisitService.find(new Page<PageVisit>(request, response), pageVisit); 
		model.addAttribute("visitCount", page.getList() == null ? 0 : page.getList().size());
        model.addAttribute("page", page);
		return "modules/momarketing/pageVisitList";
	}

	@RequiresPermissions("momarketing:pageVisit:view")
	@RequestMapping(value = "form")
	public String form(PageVisit pageVisit, Model model) {
	
		User user = UserUtils.getUser();
		if (pageVisit.getUser() == null || pageVisit.getUser().getId() <1){
			pageVisit.setUser(user);
		}
		
	
		
		model.addAttribute("pageVisit", pageVisit);
		return "modules/momarketing/pageVisitForm";
	}

	@RequiresPermissions("momarketing:pageVisit:edit")
	@RequestMapping(value = "save")
	public String save(PageVisit pageVisit, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, pageVisit)){
			return form(pageVisit, model);
		}
		pageVisitService.save(pageVisit);
		addMessage(redirectAttributes, "保存页面访问'" + pageVisit.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/momarketing/pageVisit/?repage";
	}
	
	@RequiresPermissions("momarketing:pageVisit:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		pageVisitService.delete(id);
		addMessage(redirectAttributes, "删除页面访问成功");
		return "redirect:"+Global.ADMIN_PATH+"/momarketing/pageVisit/?repage";
	}

}
